package com.logic.system.domain;

import java.lang.reflect.Field;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.logic.common.domain.BasePojo;
import com.logic.common.util.SysUtil;

/**
 * Author: omer 
 * Date  : 19-01-2016
 */
public class Notification extends BasePojo {

    private Logger logger = LoggerFactory.getLogger(this.getClass());
    
    private String sendTo;

    private String title;

    private String notiType;

    private String notiScope;

    private String content;

    private Date sentDtm;

    private String sentResult;

    private String status;

    public String getSendTo() {
        return sendTo;
    }

    public void setSendTo(String sendTo) {
        this.sendTo = sendTo;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public Date getSentDtm() {
        return sentDtm;
    }

    public void setSentDtm(Date sentDtm) {
        this.sentDtm = sentDtm;
    }

    public String getSentResult() {
        return sentResult;
    }

    public void setSentResult(String sentResult) {
        this.sentResult = sentResult;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getNotiType() {
        return notiType;
    }

    public void setNotiType(String notiType) {
        this.notiType = notiType;
    }

    public String getNotiScope() {
        return notiScope;
    }

    public void setNotiScope(String notiScope) {
        this.notiScope = notiScope;
    }

    //检查指定的参数是否为null
    //只要一个为null 返回false
    public boolean isvalid(String... fieldNames){
        if(fieldNames.length == 0){
            return true;
        }
        Class c = this.getClass();
        for(String fieldName : fieldNames){
            try {
                Field f = c.getDeclaredField(fieldName);
                f.setAccessible(true);
                Object obj = f.get(this);
                if(obj == null){
                    return false;
                }
                if(obj instanceof  String){
                    String s = (String)obj;
                    if("".equals(s)){
                        return false;
                    }
                }
            } catch (Exception e) {
                //do nothing
                logger.error("isvalid error.", e);
                return false;
            }
        }
        return true;
    }

    public Map<String, String> buildNotification(String... fieldNames){
        Class c = this.getClass();
        Map<String, String> buildedNotification = new HashMap<String, String>();
        for(String filedName : fieldNames){
            try {
                Field f = c.getDeclaredField(filedName);
                Object value = f.get(this);
                if(value != null && value instanceof Integer){
                    buildedNotification.put(filedName, String.valueOf(value));
                }else if(value != null && value instanceof Date){
                    value = new Date();
                    buildedNotification.put(filedName,  SysUtil.convertDateToString((Date)value , "yyyy-MM-dd HH:mm:ss"));
                }else if(value != null && value instanceof String){
                    buildedNotification.put(filedName, (String)value);
                }
            } catch (Exception e) {
                logger.error("buildNotification error.", e);
            }
        }
        return buildedNotification;
    }

//    public static void main(String[] args){
//        Notification notification = new Notification();
//        notification.setTitle("123");
//        notification.setContent("11");
//        notification.setSentDtm(new Date());
//
//        System.out.println(notification.buildNotification("title","content","sentDtm"));
//    }

}
